const db = require('../db/dbconfig');

// 获取所有角色信息
const getrole_skins = async(req, res) => {
    let { keyword, pagenum, pagesize } = req.query;
    let sql = `select rs.id,rs.skinname,rs.cover,rs.price,rs.islimit,role.rolename from role_skin rs 
    INNER JOIN role on rs.roleid = role.id`,
        data = null,
        msg = '请求成功！',
        status = 200,
        total = 0
    let count_sql = `select count(*) 'total' from role_skin`
    try {
        if (pagenum && pagesize) {
            if (keyword) {
                sql += ` where rs.skinname like '%${keyword}%'`
                count_sql += ` where skinname like '%${keyword}%'`
            }
            sql += ` limit ${(pagenum-1) * pagesize},${pagesize}`
            data = await db.sqlconnection(sql)
            const count_result = await db.sqlconnection(count_sql)
            total = count_result[0].total
        } else {
            status = 400
            msg = '参数必填'
        }
    } catch (error) {
        msg = `请求发生错误:${error}`
        status = 400
    } finally {
        res.status(status).send({
            data,
            total,
            meta: {
                status,
                msg
            }
        })
    }
}

// 添加皮肤
const addrole_skin = async(req, res) => {
    let { skinname, cover, price, islimit, roleid } = req.body,
        status = 200
    insert_sql = `insert into role_skin (skinname, cover, price, islimit, roleid)
        values (?,?,?,?,?)`,
        msg = '添加皮肤成功',
        data = null
    if (skinname && cover && price && islimit && roleid) { // 判断输入是否为空
        let select_sql = `select * from role_skin where skinname = '${skinname}'`
        const result = await db.sqlconnection(select_sql);
        if (result.length === 0) {
            let add_result = await db.sqlconnection(insert_sql, [skinname, cover, price, islimit, roleid])
            if (add_result.affectedRows <= 0) { // 判断新增sql语句是否添加成功
                msg = '皮肤添加失败'
                status = 400
            }
        } else {
            status = 400
            msg = '皮肤已经存在'
        }
    } else {
        status = 400
        msg = '参数必填'
    }
    res.status(status).send({
        data,
        meta: {
            status,
            msg
        }
    })
}

// 删除皮肤信息
const deleterole_skin = async(req, res) => {
    let { id } = req.query,
        sql = `delete from role_skin where id = '${id}'`,
        msg = '删除皮肤成功',
        status = 200;
    try {
        const result = await db.sqlconnection(sql)
        if (result.affectedRows <= 0) { // 根据受影响行数来判断是否删除成功
            status = 400
            msg = '删除皮肤信息失败'
        }
    } catch (error) {
        status = 400
        msg = `执行出错`
    } finally {
        res.status(status).send({
            msg,
            status
        })
    }
}

// 修改用户信息
const updaterole_skin = async(req, res) => {
    let { skinname, cover, price, islimit, roleid, id } = req.body,
        status = 200
    update_sql = `update role_skin set skinname = ?,cover = ?,price = ?,islimit = ?,roleid = ? where id = ?`,
        msg = '修改皮肤信息成功!'
    if (skinname && cover && price && islimit && roleid && id) {
        const result = await db.sqlconnection(update_sql, [skinname, cover, price, islimit, roleid, id]);
        if (result.affectedRows <= 0) {
            status = 400
            msg = '皮肤信息修改失败!'
        }
    } else {
        status = 400
        msg = '参数必填'

    }
    res.status(status).send({
        meta: {
            status,
            msg
        }
    })
}

// 查询单个皮肤信息
const get_onerole_skin = async(req, res) => {
    let { id } = req.query,
        status = 200
    let sql = `select rs.id,rs.skinname,rs.cover,rs.price,rs.islimit,role.rolename from role_skin rs
    INNER JOIN role on rs.roleid = role.id
    where rs.id =${id}`,
        data = null,
        msg = '请求成功！'
    try {
        if (id && id > 0) {
            data = await db.sqlconnection(sql)
        } else {
            status = 400
            msg = '必填参数'
        }
    } catch (error) {
        msg = `请求出错了,原因:${error}`
        status = 400
    } finally {
        res.send({
            data,
            meta: {
                status,
                msg
            }
        })
    }
};

module.exports = {
    getrole_skins,
    addrole_skin,
    deleterole_skin,
    updaterole_skin,
    get_onerole_skin
}